//
//  XLVoiceConverter.m
//  XLAudioFormatConvertTool
//
//  Created by 10150820867@qq.com on 09/02/2022.
//  Copyright (c) 2022 10150820867@qq.com. All rights reserved.
//
#import <ffmpegkit/ffmpegkit.h>
#import "XLVoiceConverter.h"

@implementation XLVoiceConverter

+(BOOL)inputFilePath:(NSString *)inputFilePath
       inputFileType:(XLAudioFileType)inputFileType
      outputFilePath:(NSString *)outputFilePath
      outputFileType:(XLAudioFileType)outputFileType
    outputSampleRate:(NSString *)outputSampleRate
    outputSampleBits:(NSString *)outputSampleBits
      outputChannels:(NSString *)outputChannels
{
    NSString *ffmpegCommand = [XLVoiceConverter createCommandWithInputFilePath:inputFilePath
                                                                 inputFileType:inputFileType
                                                                outputFilePath:outputFilePath
                                                                outputFileType:outputFileType
                                                              outputSampleRate:outputSampleRate
                                                              outputSampleBits:outputSampleBits
                                                                outputChannels:outputChannels];
    __block BOOL ret = NO;
    //[FFmpegKitConfig setLogLevel:0];
    // 信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [FFmpegKit executeAsync:ffmpegCommand
       withCompleteCallback:^(FFmpegSession *session) {
        SessionState state = [session getState];
        ReturnCode* returnCode = [session getReturnCode];
        NSLog(@"FFmpeg process exited with state %@ and code %@", [FFmpegKitConfig sessionStateToString:state], returnCode);
        ret = [returnCode isValueSuccess];
        dispatch_semaphore_signal(semaphore);
    } withLogCallback:^(Log *log) {
        NSLog(@"%@", log.getMessage);
    } withStatisticsCallback:^(Statistics *statistics) {
        
    }];
    // 等待信号量
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    return ret;
}
+ (NSString *)createCommandWithInputFilePath:(NSString *)inputFilePath
                               inputFileType:(XLAudioFileType)inputFileType
                              outputFilePath:(NSString *)outputFilePath
                              outputFileType:(XLAudioFileType)outputFileType
                            outputSampleRate:(NSString *)outputSampleRate
                            outputSampleBits:(NSString *)outputSampleBits
                              outputChannels:(NSString *)outputChannels
{
    NSMutableArray *ffmpegComandArgs = [NSMutableArray array];
    if (inputFilePath){
        [ffmpegComandArgs addObject:@"-y -i"];
        [ffmpegComandArgs addObject:inputFilePath];
    }
    else{
        NSLog(@"WARNING! inputFilePath is nil");
        return nil;
    }
    
    if (outputSampleRate){
        [ffmpegComandArgs addObject:@"-ar"];
        [ffmpegComandArgs addObject:outputSampleRate];
    }
    
    if (outputChannels){
        [ffmpegComandArgs addObject:@"-ac"];
        [ffmpegComandArgs addObject:outputChannels];
    }
    
    if (outputFilePath){
        [ffmpegComandArgs addObject:@"-vn"];
        if ([outputFilePath.lowercaseString hasSuffix:@".amr"])
        {
            if (outputFileType == XLAudioFileTypeAMRNB) {
                [ffmpegComandArgs addObject:@"-acodec libopencore_amrnb"];
            }
            else if(outputFileType == XLAudioFileTypeAMRWB) {
                [ffmpegComandArgs addObject:@"-acodec libopencore_amrwb"];
            }
        }
        else if ([outputFilePath.lowercaseString hasSuffix:@".ogg"] || outputFileType == XLAudioFileTypeOpus)
        {
            [ffmpegComandArgs addObject:@"-acodec libopus -frame_duration 20 -application voip -compression_level 10 -b:a 12k"];
        }
        else if ([outputFilePath.lowercaseString hasSuffix:@".wav"] || outputFileType == XLAudioFileTypeWAVE)
        {
            [ffmpegComandArgs addObject:@"-acodec wav"];
        }
        else if ([outputFilePath.lowercaseString hasSuffix:@".mp3"] || outputFileType == XLAudioFileTypeMp3)
        {
            [ffmpegComandArgs addObject:@"-acodec libmp3lame"];
        }
        [ffmpegComandArgs addObject:outputFilePath];
    }
    else{
        NSLog(@"WARNING! outputFilePath is nil");
        return nil;
    }
    NSString *ffmpegCommand = [ffmpegComandArgs componentsJoinedByString:@" "];
    return ffmpegCommand;
}
@end
